# coding=utf-8

import cv2

path = "./imgs/catton.jpg"

# 窗口名称
szWinName = 'src img'

# 平滑核最大半径
maxKernelRadius = 15

# 平滑核半径
kernelRadius = 1

# 平滑核窗口尺寸
kernelSize = 2 * kernelRadius + 1;

# 平滑方法
smoothMethod = 'M'


def zh_ch(string):
    return string.encode("gbk").decode(errors="ignore")


def Smoothing(img_path):
    print("用法：H - 均值滤波；B - 双边滤波；G - 高斯滤波；M - 中值滤波；Esc - 退出。\n")
    srcImgName = cv2.imread(img_path, 1)
    if srcImgName is None:
        return -1
    # 显示原始图像
    cv2.namedWindow(szWinName, cv2.WINDOW_NORMAL);
    # cv2.resizeWindow(szWinName, 800, 600)
    cv2.imshow(szWinName, srcImgName);
    dstImg = None
    dst_txt = None
    # 根据按键选择相应平滑方法实现图像平滑
    while True:
        keyVal = cv2.waitKey(1)
        if keyVal == 27:
            break
        elif keyVal == ord('H') or keyVal == ord('h'):  # Homogeneous Blur（均值滤波）
            dstImg = cv2.blur(srcImgName, (kernelSize, kernelSize));
            dst_txt = u'Homogeneous Blur（均值滤波）'
        elif keyVal == ord('B') or keyVal == ord('b'):  # Bilateral Blur（双边滤波）
            dstImg = cv2.bilateralFilter(srcImgName, kernelSize, kernelSize * 2, kernelSize / 2);
            dst_txt = u"Bilateral Blur（双边滤波）"
        elif keyVal == ord('G') or keyVal == ord('g'):  # Gaussian Blur（高斯平滑）
            dstImg = cv2.GaussianBlur(srcImgName, (kernelSize, kernelSize), 0, 0);
            dst_txt = u"Gaussian Blur（高斯平滑）"
        elif keyVal == ord('M') or keyVal == ord('m'):  # Median Blur（中值滤波）
            dstImg = cv2.medianBlur(srcImgName, kernelSize);
            dst_txt = u"Median Blur（中值滤波）"
        if dstImg is not None:
            cv2.imshow(zh_ch(dst_txt), dstImg);


if __name__ == "__main__":
    Smoothing(path);
